package com.chis.ms;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;


import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

/**
 * 
 * @Description 
 * @Company zwx
 * @author wlj
 * @version 2020年03月16日
 */
public class ThreadTest extends Thread {


    public ThreadTest(String name){
//重写构造，可以对线程添加名字
        super(name);
    }

    @Override
    public void run() {

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("account","1111111111073");
            jsonObject.put("issuer", "jmtest");
            jsonObject.put("passcode", "540983");
            jsonObject.put("sign","c634edfa1412098dbcc016626fc9648aaa86c72e43c565f8442e3cfd65e2ccce");
            String sdd = JSONUtil.toJsonStr(jsonObject);

            for(int i = 0 ; i < 1 ;i++){
                String httpRes = httppost("http://10.88.66.115:30050/checkCode", sdd);
                System.out.println(httpRes);
                Thread.sleep(100);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args){
        try {
            //并发
            for(int i = 0 ; i < 1 ; i++){
                ThreadTest threadRuning = new ThreadTest("thread"+i);
                threadRuning.start();
            }
        } catch (Exception e){
            e.printStackTrace();
        }


    }


    public static String httppost(String url, String content)  {
        try {
            URL console = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) console.openConnection();
            conn.setDoOutput(true);
            conn.addRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Content-type", "application/json");
            conn.setRequestMethod("POST");
            conn.connect();

            DataOutputStream out = new DataOutputStream(conn.getOutputStream());
            out.write(content.getBytes("utf-8"));
            // 刷新、关闭
            out.flush();
            out.close();
            InputStream is = conn.getInputStream();
            if (is != null) {
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    outStream.write(buffer, 0, len);
                }
                is.close();
                if(outStream!=null){
                    return new String(outStream.toByteArray(),"utf-8");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

}
